﻿using System;
using System.Windows;
using System.Windows.Controls;
using GameLogic.Controls;
using GameLogic.Logic;

namespace Dodger
{
    public partial class DodgerGame : UserControl, IGameFeatures
    {
         GameLoop gameLoop; // GameLoop behavior for collision and animation
         bool highscoreEntry; // Was player good enough for a highscore entry
        
        // IGameFeatures event handlers
        public event EventHandler<ScoresChangedEventArgs> ChangeScoresFired;
        public event EventHandler<LivesChangedEventArgs> ChangeLivesFired;
        //public event EventHandler<HighscoreEntryArgs> HighscoreEntryFired;
        public event EventHandler PauseTimerFired;
        public event EventHandler ResumeTimerFired;
        public event EventHandler GameOverFired;
        public event EventHandler NewGameFired;


        public DodgerGame()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(OnLoaded);
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Find the GameLoop
            GameLoopFinder glf = new GameLoopFinder(LayoutRoot);
            gameLoop = glf.GameLoop;
        }

        public void Start()
        {
            if (gameLoop != null)
                gameLoop.StartGameLoop();

            // Show game screen
            VisualStateManager.GoToState(this, "Game", false);
            highscoreEntry = false;
        }

        public void Stop()
        {
            if (gameLoop != null)
                gameLoop.ResetGameLoop();
        }

        public void Pause()
        {
            if (gameLoop != null)
                gameLoop.PauseGameLoop();
        }

        public void Resume()
        {
            if (gameLoop != null)
                gameLoop.StartGameLoop();
        }

        public void ChangeScores(int p)
        {
            if (ChangeScoresFired != null)
            {
                ScoresChangedEventArgs args = new ScoresChangedEventArgs();
                args.Value = p;
                ChangeScoresFired(this, args);
            }
        }

        public void ChangeLives(int p)
        {
            // If player lost a live ...
            if (p < 0)
            {
                // ... reset the game loop
                this.Stop();
                // tell the game container to pause timer
                if (this.PauseTimerFired != null)
                    this.PauseTimerFired(this, EventArgs.Empty);

                // Show life lost screen
                VisualStateManager.GoToState(this, "LifeLost", false);
            }

            // Tell the game container to change lives
            if (ChangeLivesFired != null)
            {
                LivesChangedEventArgs args = new LivesChangedEventArgs();
                args.Value = p;
                ChangeLivesFired(this, args);
            }          
        }

        public void GameOver(GameOverEventArgs args)
        {
            // Tell game container that game is over
            if (this.GameOverFired != null)
            {
                this.GameOverFired(this, EventArgs.Empty);
            }

            // Show game over screen
            VisualStateManager.GoToState(this, "Over", false);
            //  Hide _highscoreBox
            _highscoreBox.Visibility = Visibility.Collapsed;

            // If player was good enough for the highscore list 
            if (args.HighscoreRank > 0)
            {
                // Show highscore message and ask for name
                _highscoreBox.Visibility = Visibility.Visible;
                _highscoreRank.Text = args.HighscoreRank.ToString();
                highscoreEntry = true;
            }
        }

        private void _continue_Click(object sender, RoutedEventArgs e)
        {
            // Start the game loop
            this.Start();
            // tell the game container to resume timer
            if (this.ResumeTimerFired != null)
                this.ResumeTimerFired(this, EventArgs.Empty);

            // Show game screen
            VisualStateManager.GoToState(this, "Game", false);
        }

        //private void _newGame_Click(object sender, RoutedEventArgs e)
        //{
        //    // If player was good enough for highscore entry
        //    if (highscoreEntry && this.HighscoreEntryFired != null)
        //    {
        //        HighscoreEntryArgs args = new HighscoreEntryArgs();
        //        args.Player = _playerName.Text;
        //        this.HighscoreEntryFired(this, args);
        //    }

        //    // Tell the game container that we want to start a new game
        //    if (this.NewGameFired != null)
        //    {
        //        this.NewGameFired(this, EventArgs.Empty);
        //    }
        //}


        public bool IsTimeBased
        {
            get { return true; }
        }

        public bool IsLifeBased
        {
            get { return true; }
        }

        public string GameName
        {
            get { return "Dodger"; }
        }


        public TimeSpan GameDuration
        {
            get { throw new NotImplementedException(); }
        }
    }
}

       